
// include the library code:
#include <LiquidCrystal.h>
#include <SD.h>
#include <OneWire.h>
#include <DallasTemperature.h>

//----------------------LCD--------------------------------------------------
/*
DISPLAY LCD
  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 */
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//----------------------LCD--------------------------------------------------

//----------------------ONE-WIRE---------------------------------------------
//definisco il pin del one-wire
#define ONE_WIRE_BUS 3
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
//Imposto l'indirizzo del sensore DS18B20
DeviceAddress termometro = { /*trovare il seriale da inserire*/ };
//----------------------ONE-WIRE---------------------------------------------

//----------------------SD---------------------------------------------------
/*The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11  -- NO
 ** MISO - pin 12  -- NO
 ** CLK - pin 13 -- NO
 ** CS - pin 10 -- OK
 */
//DEVO TROVARE ALTRI PIN PER LO SHIELD SD, QUELLI STANDARD SONO GIA' USATI

const int chipSelect = 4;

//----------------------SD---------------------------------------------------
// definisco l'uscita del contatto di accensione della caldaia
int caldaia = 6;

//Preparo la matrice per la programmazione settimanale
int programma[7][40];

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  
  //definisco il pin caldaia come uscita
  pinMode(caldaia, OUTPUT);
  
  // start serial port - per monitorare via seriale i dati
  Serial.begin(9600);
  // Start up the library
  
  sensors.begin();
  // set the resolution to 10 bit (massimo 12)
  sensors.setResolution(termometro, 10);
  
  //Inizializzo la SD
  Serial.print("Inizializzazione SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  if (!SD.begin(chipSelect)) {
    Serial.println("Errore : Inizializzazione fallita - Carta non idonea o non presente");
    // don't do anything more:
    return;
  }
  Serial.println("SD Inizializzata");
}

//Funzione per la letturadel programma settimanale delle temperature
void readPrg(){
  int j = 0;
  int counter = 0;
  String serialDataIn = String("");
  String data[7][24];
  File filePrg = SD.open("Programma.dat");
  while (filePrg.available()) {
     char inbyte = (char)filePrg.read(); 
     if(inbyte >= '0' & inbyte <= '9'){
       serialDataIn += inbyte;
       }
     if (inbyte == ','){  // Handle delimiter
       data[j][counter] = String(serialDataIn);
       serialDataIn = String("");
       counter = counter + 1;
       Serial.print("Giorno ");       
       Serial.print(j);
       Serial.print(" Ora ");
       Serial.print(counter);
       Serial.print(" Setpoint : ");
       Serial.print(serialDataIn);
       
     }
     if(inbyte ==  '\r'){  //fine linea
       j++;
     }        
  }
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Errore termometro");
    lcd.print("Errore termometro");
  } else {
    // open the file. note that only one file can be open at a time,
    // so you have to close this one before opening another.
    File dataFile = SD.open("datalog.txt", FILE_WRITE);
    // if the file is available, write to it:
    if (dataFile) {
      /*aggiungere data e ora*/
      dataFile.println(tempC);
      dataFile.close();
    } else {
      Serial.println("error opening datalog.txt");
    } 
    lcd.clear();
    lcd.print("Temperatura :");
    lcd.setCursor(0, 1);  
    lcd.print(tempC);
    lcd.print(" C");
    Serial.print("C: ");
    Serial.print(tempC);
  }
}

void loop() {
  //faccio la misura ogni X secondi (1000 = 1 sec)
  delay(30000);//30 sec
  Serial.print("Getting temperatures...\n\r");
  sensors.requestTemperatures();
  Serial.print("Inside temperature is: ");
  printTemperature(termometro);
}

